package com.yeyks.rocketmq.service.customer;

import cn.hutool.json.JSONUtil;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.yeyks.common.em.WebSocketTypeEnum;
import com.yeyks.commonReference.append.rocketmq.pojo.RocketMQWebsocketBasePojo;
import com.yeyks.websocket.SendMessageBean;
import com.yeyks.websocket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author fuchangshuai
 * @date 2019/08/09 16:06
 */
@Slf4j
@Component
public class WebSocketListener implements MessageListener {

    @Autowired
    private WebSocket robWebSocket;
    @Autowired
    private CommonDisposeService commonDisposeService;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            Long startTime = System.currentTimeMillis();
            byte[] body = message.getBody();
            //获取到接收的消息，由于接收到的是byte数组，所以需要转换成字符串
            String msg = new String(body);
            //处理业务逻辑
            disposeLogic(msg);
            log.info("webSocket检测：监听到的消息：message = {}", msg);
            Long endTime = System.currentTimeMillis();
            log.info("webSocket检测：单次消费耗时：" + (endTime - startTime) / 1000);
        } catch (Exception e) {
            log.error("MessageListener.consume error:" + e.getMessage(), e);
        }
        return Action.CommitMessage;
    }

    /**
     * 3.4手动自动抢单，处理业务逻辑
     *
     * @param msg 监听到的信息
     */
    private void disposeLogic(String msg) {
        try {
            RocketMQWebsocketBasePojo webSocket = JSONUtil.toBean(msg, RocketMQWebsocketBasePojo.class);
            if (null != webSocket) {
                if (WebSocketTypeEnum.ROB_ORDER.value().equals(webSocket.getType())) {
                    robWebSocket.sendMessageToUser(webSocket.getUserId(), SendMessageBean.getRobOrderMsg(webSocket.getBody()));
                }
                if (WebSocketTypeEnum.ROB_ORDER_RESULT.value().equals(webSocket.getType())) {
                    robWebSocket.sendMessageToUser(webSocket.getUserId(), SendMessageBean.getRobOrderSuccessMsg(webSocket.getBody()));
                }
                if (WebSocketTypeEnum.ORDER.value().equals(webSocket.getType())) {
                    robWebSocket.sendMessageToUser(webSocket.getUserId(), webSocket.getBody());
                }
                //b端web端统一监听b2c订单下单成功并处理逻辑(为了不新建groupId，暂时这样写)
                if (WebSocketTypeEnum.B2C_ORDER_SUCCESS.value().equals(webSocket.getType())) {
                    log.info("b2c订单下单成功 message = {}", webSocket);
                    commonDisposeService.pushMessage(webSocket.getOrderNo());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("webSocket检测：处理监听消息异常：msg = {}", msg);
        }
    }
}
